package com.shujia.custom.udfdemo;

import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

public class MyUDFDemo3 extends GenericUDF {
    String output = "";

    /*
        initialize方法做的是初始化，主要是设置输出的类型

        PrimitiveObjectInspectorFactory去获取返回的数据类型
     */
    @Override
    public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
        output = "";
        return PrimitiveObjectInspectorFactory.javaStringObjectInspector;
    }

    /*
            evaluate: 编写主要逻辑
     */
    @Override
    public Object evaluate(DeferredObject[] arguments) throws HiveException {
        if(arguments.length==1){
            String input = arguments[0].get().toString();
            output = "数加: "+input+" 666";
        }

        return output;
    }

    @Override
    public String getDisplayString(String[] children) {
        return "这是自己编写的自定义函数UDF";
    }
}
